package com.baidu.service.impl;

import com.baidu.common.Rest;
import com.baidu.entity.SysUser;
import com.baidu.mapper.SysUserMapper;
import com.baidu.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper,SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public Rest register(SysUser user) {
        SysUser byUsername = userMapper.findUserByUsername(user.getUsername());
        if (byUsername == null){
            return userMapper.insertOne(user) ? Rest.Success(true) : Rest.faild("注册失败");
        }else{
            return Rest.faild("用户已存在");
        }
    }

    public Rest destster(Integer id) {
        SysUser byUsername = userMapper.findUserByUsername(String.valueOf(id));
        if (byUsername == null){
            return userMapper.deleteUserByUsername(id) ? Rest.Success(true) : Rest.faild("注册失败");
        }else{
            return Rest.faild("用户不存在");
        }
    }

    public Rest updataster(SysUser user) {
        SysUser byUsername = userMapper.findUserByUsername(String.valueOf(user.getId()));
        if (byUsername == null){
            return userMapper.updataUserByUsername(user) ? Rest.Success(true) : Rest.faild("更新失败");
        }else{
            return Rest.faild("用户不存在");
        }
    }

    @Override
    public List<SysUser> selectList() {
        return userMapper.selectListAll();
    }
}
